package com.xingchi.tornado.test.controller;

import com.xingchi.tornado.basic.Result;
import com.xingchi.tornado.storage.core.FileStorageService;
import com.xingchi.tornado.storage.core.model.FileUploadHolder;
import com.xingchi.tornado.storage.core.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 *
 * @author xingchi
 * @date 2024/5/5 17:52
 */
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        FileUploadHolder holder = FileUploadHolder.wrap(file);
        fileUploadService.uploadFile(holder);
        return Result.ok();
    }

    @GetMapping
    public Result<String> getUrl(@RequestParam("path") String path) {
        return Result.ok(fileStorageService.getUrl(path));
    }

}
